//
//  BasePickView.m
//  New_Runway
//
//  Created by mack on 2018/1/27.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "BasePickView.h"

@implementation BasePickView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _pickerViewHeight      = 300;
        
        
        self.bounds = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.64];
        self.layer.opacity = 0.0;
        
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:tap];
        
        
        [self addSubview:self.contentView];
        [self.contentView addSubview:self.topview];
        [self.contentView addSubview:self.pickerView];
        [self.contentView addSubview:self.cancelButton];
        [self.contentView addSubview:self.confirmButton];
        
        [self initPickView];
    }
    return self;
}

-(void)initPickView{
    
}

- (void)clickConfirmButton
{
    [self disMiss];
}

- (void)clickCancelButton
{
    [self disMiss];
}




- (void)show
{
    self.isShow = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
//
//
//    CGRect frameContent =  self.contentView.frame;
//
//    frameContent.origin.y -= self.contentView.frame.size.height;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:1.0];
//        self.contentView.frame = frameContent;
        self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT() - self.pickerViewHeight, SCREEN_WIDTH(), self.pickerViewHeight);
        
    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)disMiss
{

    self.isShow = NO;
    CGRect frameContent =  self.contentView.frame;
    frameContent.origin.y += self.contentView.frame.size.height;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:0.0];
//        self.contentView.frame = frameContent;
        self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT(), SCREEN_WIDTH(), self.pickerViewHeight);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}



- (void)setPickerViewHeight:(CGFloat)pickerViewHeight
{
    _pickerViewHeight = pickerViewHeight;
    self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT(), self.contentView.frame.size.width, pickerViewHeight);
    _pickerView.frame = CGRectMake(0, 44, self.contentView.frame.size.width, pickerViewHeight - 44);
    
}

- (UIView *)contentView
{
    if (!_contentView) {
        
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH(), SCREEN_WIDTH(), self.pickerViewHeight)];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
    }
    return _contentView;
}


- (UIPickerView *)pickerView
{
    if (!_pickerView) {
        
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,  0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
        [_pickerView setBackgroundColor:[UIColor whiteColor]];
        
    }
    return _pickerView;
}

-(UIView *)topview{
    if (!_topview) {
        _topview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH(), 44)];
        _topview.backgroundColor = mainColor();
        
    }
    return _topview;
    
}
- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        
        _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 44, 44)];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:whiteColor() forState:UIControlStateNormal];
        
        [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}


- (UIButton *)confirmButton
{
    if (!_confirmButton) {
        
        _confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.frame.size.width - self.cancelButton.frame.size.width - self.cancelButton.frame.origin.x, self.cancelButton.frame.origin.y, self.cancelButton.frame.size.width, self.cancelButton.frame.size.height)];
        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:whiteColor() forState:UIControlStateNormal];
        
        [_confirmButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_confirmButton addTarget:self action:@selector(clickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}



@end
